import {AMapManager,} from 'vue-amap';
import {cbs, phoneReg, pwdReg} from '../../config/settings.js';
import md5 from 'js-md5';
import SendCode from '../Common/SendCode';

let amapManager = new AMapManager();
let pos = {};
let geo;
let searchDom = null;

module.exports = {
	phoneReg: phoneReg,
	pwdReg: pwdReg,
	data() {
		let valiPhone = (rule, value, cb) => {
			if(!value) {
				cb(new Error('请输入手机号'));
			}else if(!this.$options.phoneReg.test(value)) {
				cb(new Error('请输入有效号码'))
			}else {
				cb()
			}
		};
		let noEmpty = (rule, value, cb) => {
			if(!value) {
				if(!pos.lng) {
					cbs.submitError.call(this, `请选择经纬度`);
				}else {
					this.registerForm.longitude = pos.lng;
					this.registerForm.latitude = pos.lat;
				}
			}else {
				cb()
			}
		};

		return {
			amapManager,
			registerForm: {
				phoneNumber: '',
				verificationCode: '',
				detailAddress: '',
				password: '',
				longitude: '',
				latitude: '',
				name: '',
				address: '',
			},
			registerRules: {
				phoneNumber: [{
					validator: valiPhone,
					trigger: 'blur'
				}],
				verificationCode: [{
					validator: (rule, value, cb) => {
						if(value === '') {
							cb(new Error('请输入验证码'));
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}],
				detailAddress: [{
					validator: (rule, value, cb) => {
						if(value === '') {
							cb(new Error('请输入详细地址'));
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}],
				password: [{
					validator: (rule, value, cb) => {
						if(value === '') {
							cb(new Error('请输入密码'));
						}else if(!this.$options.pwdReg.test(value)) {
							cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}],
				longitude: [{
					validator: noEmpty,
					trigger: 'blur'
				}],
				latitude: [{
					validator: noEmpty,
					trigger: 'blur'
				}],
				name: [{
					validator: (rule, value, cb) => {
						if(value === '') {
							cb(new Error('请输入水店名字'));
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}]
			},
			center: [113.256874, 23.18562],
			zoom: 12,
			events: {
				init(o) {
					geo = new AMap.Geocoder({
						radius: 1000,
						extensions: 'all'
					})
				},
				click(e) {
					pos = e.lnglat;
					geo.getAddress(pos, (status, result) => {
						if (status === 'complete' && result.info === 'OK') {
							searchDom.val(result.regeocode.formattedAddress)
			            }
					})
				}
			},
			markers: [
				[113.256874, 23.18562]
			]
		}
	},
	methods: {
		/**
         * 手机号是否已注册
         */
		validatePhone(name) {
			if(!this.$options.phoneReg.test(this.registerForm.phoneNumber)){
				this.$refs[name].validateField('phoneNumber');
			}else {
				let formData = this.$$formData({'phoneNumber': this.registerForm.phoneNumber});
				this.$refs.code.changeStatus(0);

				this.$$api_authen_auth(formData, res => {
					if(res.event === 2) {
						cbs.submitError.call(this, "该号码已注册");
						this.$refs.code.changeStatus(-1);
					}
				}, {
					tokenFlag: true
				})
			}
		},
		/**
         * 搜索地址
         */
		onSearchResult(pois) {
			let latSum = 0,
				lngSum = 0,
				len = pois.length;
			if(len) {
				pois.forEach(poi => {
					let {lng, lat} = poi;
					latSum += lat;
					lngSum += lng;
				});
				let center = {
					lng: lngSum / len,
					lat: latSum / len
				}

				this.markers[0] = [center.lng, center.lat];
				this.center = [center.lng, center.lat];
				this.registerForm.longitude = center.lng;
				this.registerForm.latitude = center.lat;
			}
		},
		/**
         * 提交
         */
		onPost(name) {
			this.$refs[name].validate(valid => {
				if(valid) {
					this.registerForm.address = searchDom.val();
					this.registerForm.password = md5(this.registerForm.password);

					let data = this.$$formData(this.registerForm);

					this.$$api_authen_register(data, res => {
						if(res.event === 0) {
							this.$router.push('/authen');
							cbs.submitSuccess.call(this, '注册成功，请填写认证信息')
						}else if(res.event === 2) {
							cbs.submitError.call(this, `用户已存在`);
							this.registerForm.password = '';
						}else {
							cbs.submitError.call(this, res.msg);
							this.registerForm.password = '';
						}
					})
				}
			})
		},
		/**
         * 重置
         */
		onBack() {
			this.$router.push('/login');
		},
	},
	computed: {
		codeText() {
			return this.timer > 0 ? `请${this.timer}秒后重发` : '发送验证码';
		}
	},
	components: {
		SendCode
	},
	created() {
	},
	mounted() {
		searchDom = $('.search-box-wrapper input');
	}
}